﻿/*******************************************************
 *  戒毒治疗人员 相关操作js
 * <p>Title: treatManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2019年1月3日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//戒毒人员管理
var options,unitzTree, selNode, $table, addOrEditUrl, orgId = null, isParent = false, folder_path, issetOut = false;
var addIndex;
var treatManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        treatManager.bindZtree();
        treatManager.initSelect();
        treatManager.initTable();
        treatManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化树
    bindZtree: function () {
        var setting = {
            view: {
                showIcon: true,
                selectedMulti: false,
                fontCss: getFontCss
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                dataType: "text",
                type: "post",
                url: ctx + '/SysOrg/GetOrgTreeDataByRelation',
                autoParam: ["id"]
            },
            data: {
                key: { title: "name", name: "name" },
                simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 }
            },
            check: {
                enable: false,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "p", "N": "ps" }
            },
            callback: {
                onAsyncError: function () {
                    layer.msg("初始化单位结构树失败~", { icon: 5 });
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    //树加载完成后执行
                    var obj = unitzTree.getNodes()[0];
                    if (obj) {
                        issetOut = true;
                        unitzTree.selectNode(obj);
                        unitzTree.setting.callback.onClick(null, unitzTree.setting.treeId, obj);//模拟点击事件 
                        if (obj.children && obj.children.length > 0)
                            unitzTree.expandNode(obj);
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    $('#unitTree a').css("color", "#333");
                    $('#' + treeNode.tId + '_a').css("color", "#ffffff");
                    isParent = treeNode.isParent;
                    orgId = treeNode.id;
                    selNode = treeNode;//选中节点
                    folder_path = treeNode.folder_path;
                    if (issetOut)
                    { setTimeout(function () { $table.bootstrapTable('refresh'); issetOut = false; }, 1000); }
                    else
                        $table.bootstrapTable('refresh');
                }
            }
        };
        unitzTree = $("#unitTree").initzTreeView(setting, 'ushow');
    },
    //初始化医院下拉
    initSelect: function () {
        // 初始化医院下拉
        $.ajax({
            url: ctx + '/BaneHospital/GetHospitalDataByPer',
            type: "post",
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.Statu != 0)
                    return false;
                var itemStr = "<option value=''>空</option>";
                $.each(data.Data, function (i, item) {
                    itemStr += "<option value='" + item.hos_id + "'>" + item.hos_name + "</option>";
                });
                $('#hosId').append(itemStr);
                $('#hosId').selectpicker('refresh');
            }
            //,
            //error: function () {
            //    layer.msg('数据异常~', { icon: 5 });
            //}
        });
        //初始化全部医院下拉
        $.ajax({
            url: ctx + '/BaneHospital/GetAllHospitalData',
            type: "post",
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.Statu != 0)
                    return false;
                var itemStr = "<option value=''>空</option>";
                $.each(data.Data, function (i, item) {
                    itemStr += "<option value='" + item.hos_id + "'>" + item.hos_name +"<"+item.folder_name+">" + "</option>";
                });
                $('#hoid').append(itemStr);
                $('#hoid').selectpicker('refresh');
            }
            //,
            //error: function () {
            //    layer.msg('数据异常~', { icon: 5 });
            //}
        });
        $('.selectpicker').selectpicker({ size: 3 });
    },
    //初始化Table
    initTable: function () {
        options = {
            url: ctx + "/BaneTreatPerson/GetBaneHospitalData",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showExport: true,//显示导出
            exportTypes: ['excel'],
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    orgId: orgId,//选中单位(组织机构)
                    folder_path: folder_path,
                    isParent: isParent ? 1 : 0,
                    hos_id: $('#hosId').val(),//医院                     
                    keyword: encodeURI($('#treatName').val()) //关键字
                }
                return params;
            },
            columns: [
            {
                radio: true
            },
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        },
	        {
	            field: 'user_name',
	            title: '用户名',
	            align: 'center'
	        },
	        {
	            field: 'user_sex',
	            title: '性别',
	            align: 'center'
	        },
	        {
	            field: 'user_age',
	            title: '年龄',
	            align: 'center'
	        }, {
	            field: 'user_identify',
	            title: '身份证',
	            align: 'center'
	        }, {
	            field: 'hos_code',
	            title: '治疗卡号',
	            align: 'center'
	        }, {
	            field: 'move_phone',
	            title: '联系电话',
	            align: 'center'
	        }, {
	            field: 'dose_num',
	            title: '当前剂量（ml/次）',
	            align: 'center'
	        }, {
	            field: 'ck_date',
	            title: '上次服药时间',
	            align: 'center'
	        }],
            onClickRow: function (row, $element) {
                if (row.user_id) {
                    $('#transferHos').attr("disabled", false);
                    $('#treatExportExcel').attr("disabled", false);
                }
            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            $table.bootstrapTable('refresh', options);
        });
        //编辑
        $('#treatEdit').click(function () {
            var row = $table.bootstrapTable('getSelections');
            if (row!=null && row.length == 0) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            //清空表单
            $('#orgFormTable').resetHideValidForm();
            layer.open({
                title: ['编辑剂量', 'font-size:18px;'],
                type: 1,
                content: $('#orgform'),
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['确定', '取消'],
                area: ['550px', '300px'],
                success: function (li, o) {
                    if (row != null) {
                        $('#orgFormTable')[0].reset();//重置表单
                        $('#orgFormTable').LoadForm(row[0]);//表单填充数据
                    }
                },
                yes: function (li, o) {
                    if ($('#orgFormTable').valid()) {
                        //验证通过
                        $('#orgFormTable').ajaxSubmit({
                            url: ctx + "/BaneTreatPerson/EditHosTreat",
                            type: "post",
                            dataType: "json",
                            beforeSubmit: function (arr, $form, options) {
                                layer.msg("提交数据~", { icon: 1, time: 5000 });
                            },
                            success: function (result, status, xhr, $form) {
                                if (result.Statu === 0) {
                                    layer.closeAll();
                                    $table.bootstrapTable('refresh', options);
                                    layer.msg('保存成功', { icon: 1 });
                                }
                                else
                                    layer.alert(result.Msg, { icon: 5 });
                            }
                            //,
                            //error: function (xhr, status, error, $form) {
                            //    layer.msg("保存失败~", { icon: 5 });
                            //}
                        });
                    }
                },
                cancel: function (li, o) {

                }
            });
        });
        //导出
        $('#treatExportExcel').on('click', function () {
            //jsToExcel('exampleTableToolbar','', '治疗人员记录.xls');
            var $data = $table.bootstrapTable('getOptions');
            if (!$data || $data.data.length <= 0) {
                layer.msg("没有需要导出的数据！", { icon: 5 });
                return false;
            }
            layer.msg("数据获取中...", { icon: 6, time: 9000 });
            var hos_id = $('#hosId').val(), keyword = $('#treatName').val();
            keyword = (keyword) ? encodeURI(keyword) : "";
            hos_id = (hos_id) ? hos_id : 0;
            params = "folder_path=" + folder_path + "&hos_id=" + hos_id + "&keyword=" + keyword;
            var personExport = $('#personExport');
            personExport.attr("href", ctx + "/BaneTreatPerson/ExportRecordData?" + params);
            document.getElementById("personExport").click();
            return false;
        });
        //转诊
        $('#transferHos').on('click', function () {
            var row = $table.bootstrapTable('getSelections');
            if (row != null && row.length == 0) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            //清空表单
            $('#orgHosFormTable').resetHideValidForm();
            layer.open({
                title: ['编辑剂量', 'font-size:18px;'],
                type: 1,
                content: $('#orgHosform'),
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['确定', '取消'],
                area: ['550px', '300px'],
                success: function (li, o) {
                    $('#userId').val(row[0].user_id);
                    $('#hoid').selectpicker('val', '');
                    $('#hoid').selectpicker('refresh');
                },
                yes: function (li, o) {
                    var hos_id = $('#hoid').val(), userId = $('#userId').val();
                    if (!hos_id) {
                        layer.msg("请选择转入医院！", { icon: 5 });
                        return false;
                    }
                    //验证通过
                    $('#orgHosFormTable').ajaxSubmit({
                        url: ctx + "/BaneTreatPerson/MoveToHosId",
                        type: "post",
                        data: { hos_id: hos_id, user_id: userId },
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("提交数据~", { icon: 1, time: 5000 });
                        },
                        success: function (result, status, xhr, $form) {
                            if (result.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.msg('保存成功', { icon: 1 });
                            }
                            else
                                layer.alert(result.Msg, { icon: 5 });
                        }
                        //,
                        //error: function (xhr, status, error, $form) {
                        //    layer.msg("保存失败~", { icon: 5 });
                        //}
                    });
                },
                cancel: function (li, o) {

                }
            });
        });
    },
    //关闭新增窗口
    closeAddForm: function () {
        if (addIndex)
            layer.close(addIndex);
    }
}